/**
 * 
 */
package com.nail.zhaogr.action;

import java.util.List;

import javacommon.base.BaseStruts2Action;
import javacommon.util.Md5Bean;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.util.WebUtils;

import com.nail.zhaogr.model.Rights;
import com.nail.zhaogr.model.Users;
import com.nail.zhaogr.service.RightsManager;
import com.nail.zhaogr.service.UsersManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class LoginAction  extends BaseStruts2Action {
	
	protected String Login_success = "/pages/ronail/index.jsp";
	protected String Login_fail = "/pages/ronail/login.jsp";
	protected String Login_left = "/pages/ronail/left.jsp";

	private Users users;

	private UsersManager usersManager;

	private RightsManager rightsManager;

	private List<Rights> rightList;

	public String index() {
		return Login_fail;
	}

	public String left() {
		//rightList = (List) WebUtils.getSessionAttribute(ServletActionContext.getRequest(), "rightList");
		return Login_left;
	}

	public String exit() {
		ServletActionContext.getRequest().removeAttribute("userInfo");
		ServletActionContext.getRequest().removeAttribute("rightList");
		return Login_fail;
	}

	public String login() {

		users.setPassword(Md5Bean.getInstance().getMD5ofStr(users.getPassword()));
		if (usersManager.checkLoginName(users.getLoginName())) {
			users = usersManager.login(users);
			if (users != null) {
				WebUtils.setSessionAttribute(ServletActionContext.getRequest(), "userName", "userName");
				WebUtils.setSessionAttribute(ServletActionContext.getRequest(), "userInfo", users);
				/*if (users.getLoginName().equals("admin")) {
					rightList = rightsManager.findByAvailableRights();
				} else {
					rightList = rightsManager.findRightByEmployeeId(users.getUserId());
				}*/
				WebUtils.setSessionAttribute(ServletActionContext.getRequest(), "rightList", rightList);
				return Login_success;
			} else {
				addActionMessage("密码错误，请重试！");
				return Login_fail;
			}

		} else {
			addActionMessage("用户名不存在");
			return Login_fail;
		}

	}

	

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public void setUsersManager(UsersManager usersManager) {
		this.usersManager = usersManager;
	}

	/**
	 * @return the rightsManager
	 */
	public RightsManager getRightsManager() {
		return rightsManager;
	}

	/**
	 * @param rightsManager
	 *            the rightsManager to set
	 */
	public void setRightsManager(RightsManager rightsManager) {
		this.rightsManager = rightsManager;
	}

	/**
	 * @return the rightList
	 */
	public List<Rights> getRightList() {
		return rightList;
	}

	/**
	 * @param rightList
	 *            the rightList to set
	 */
	public void setRightList(List<Rights> rightList) {
		this.rightList = rightList;
	}

}
